package wat.serwerapp.connection;

import java.security.InvalidKeyException;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.crypto.SecretKey;
import wat.serwerapp.usermgmt.UserManager.User;


class NoSuchSessionException extends Exception {
}

/**
 *
 * @author Tarni & P.B. & L.G. & K.K.
 */
public final class SessionManager {
/*
   private class SessionInfo {   

      SecretKey key;
      User user;
      Date timeLoggedIn;
      Date timeLastActive;
      Date timeLoggedOut;

      SessionInfo(SecretKey k) {
         key = k;
         timeLastActive = new Date();
      }

      @Override
      public String toString() {
         String res = "user: " + user.getName() + "; logged in " +
                 timeLoggedIn + ", logged out " + timeLoggedOut;
         return res;
      }
   }

   static int idleLimit = 65;  // max czas bezczynnosci przed odlaczeniem sesji (minutes)
   private static SessionManager instance;
   private Map<String, SessionInfo> sessions;

   private SessionManager() {
      //private constructor
      sessions = new HashMap<String, SessionInfo>();
   }

   private void deleteOldies() {
      long pomIdle = 35;
      for (String key : sessions.keySet()) {
         long milis = sessions.get(key).timeLastActive.getTime() - (new Date()).getTime();
         long mins = milis/60000;
         if (mins > idleLimit) sessions.remove(key);
         else {
            // wybranie sesji, dla ktorej najrychlej minie czas
            if (idleLimit - mins < pomIdle)  pomIdle = (idleLimit - mins);
            pomIdle = Math.max(pomIdle, 3);
         }
      }
      // TODO pomIdle - za tyle minut ponownie uruchomic deleteOldies
   }

   public static final SessionManager getInstance() {
      if (instance == null) {
         instance = new SessionManager();
      }
      return instance;
   }

   public void storeKey(String session, SecretKey secretKey)  throws InvalidKeyException {
      if (secretKey == null)  throw new InvalidKeyException();
      if (sessions.containsKey(session)); //TODO  throw SessionAlreadyExists();

      sessions.put(session, new SessionInfo(secretKey));
   }

   public SecretKey retrieveKey(String session) throws NoSuchSessionException {
      SessionInfo si = sessions.get(session);
      if (si == null)  throw new NoSuchSessionException();
      return si.key;
   }

   public User retrieveUser(String session) throws NoSuchSessionException {
      SessionInfo si = sessions.get(session);
      if (si == null)  throw new NoSuchSessionException();
      return si.user;
   }

   public void assignVerifiedUser(String session, User user) throws NoSuchSessionException {
      SessionInfo si = sessions.get(session);
      if (si == null)  throw new NoSuchSessionException();
      if (si.user != null); // TODO  throw UserAlreadyLoggedIn

      si.user = user;
      si.timeLoggedIn = new Date();
      si.timeLastActive = si.timeLoggedIn;
   }

   public void keepSessionAlive(String session) throws NoSuchSessionException {
      SessionInfo si = sessions.get(session);
      if (si == null)  throw new NoSuchSessionException();
      if (si.user == null); //TODO throw no such session or no user logged in;
      si.timeLastActive = new Date();
   }

   public void closeSession(String session) throws NoSuchSessionException {
      SessionInfo si = sessions.get(session);
      if (si == null)  throw new NoSuchSessionException();
      if (si.user == null); //TODO throw no such session or no user logged in;
      si.timeLoggedOut = new Date();
      Logger.getLogger(SessionManager.class.getName()).log(Level.INFO, "user logout\n ("+si.toString()+")", si);
      sessions.remove(session);
   }

   public void abandonSession(String session) {
      sessions.remove(session);
   }*/
}
